package com.gouqi.mall.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类
 * @author:xiaofei
 * @time:2020-04-23
 */
public class DateUtils {
    /**
     * Timestamp -> Date
     * 最后格式：Thu Dec 20 12:34:23 CST 2018
     * @author huzi
     * @date   2019年1月18日
     * @param  timestamp时间戳
     * @return
     */
    public static Date timeToDate(Timestamp timestamp) {

        // Timestamp -> String
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(timestamp);

        // String -> Date
        Date date = new Date();
        //注意format的格式要与日期String的格式相匹配
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * data类型数据转TimeStamp
     * @param date
     * @return
     */
    public static Timestamp dateToTimeStamp(Date date) {
        Timestamp timestamp = new Timestamp(date.getTime());
        return timestamp;
    }


    public static void main(String[] args) {
        Timestamp timestamp = dateToTimeStamp(new Date());
        System.out.println(timestamp.toString());//2020-03-22 16:49:35.11
    }
}
